package com.example.environment.controller;

import com.example.environment.pojo.EncryptedLoginData;
import com.example.environment.service.AuthService;
import com.example.environment.tools.JwtUtil;
import com.example.environment.tools.RsaUtils;
import com.example.environment.tools.common.ResultMsg;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.crypto.Cipher;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;

@RestController
@RequestMapping("/auth")
public class LoginController {
    @Autowired
    private AuthService authService;

    @PostMapping("/svLogin")
    public ResultMsg svLogin(EncryptedLoginData encryptedData, HttpServletRequest httpServletRequest) {
        try {
            byte[] encryptedUsername = Base64.getDecoder().decode(URLDecoder.decode(encryptedData.getEncryptedUsername(), "UTF-8"));
            byte[] encryptedPassword = Base64.getDecoder().decode(URLDecoder.decode(encryptedData.getEncryptedPassword(), "UTF-8"));
            // 使用前端相同的填充方式进行解密
            Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            rsaCipher.init(Cipher.DECRYPT_MODE, RsaUtils.getKeyPair().getPrivate());
            // 对RSA加密解密
            String username = new String(rsaCipher.doFinal(encryptedUsername), StandardCharsets.UTF_8);
            String password = new String(rsaCipher.doFinal(encryptedPassword), StandardCharsets.UTF_8);

            System.out.println("Decrypted username: " + username);
            System.out.println("Decrypted password: " + password);
            if(httpServletRequest.getAttribute("id") == null||!httpServletRequest.getAttribute("id").equals(username)){
                return authService.SvLogin(username, password);
            }
            return ResultMsg.successOp("已登录");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.failOp("出现异常");
    }

    @PostMapping("/staffLogin")
    public ResultMsg staffLogin(EncryptedLoginData encryptedData, HttpServletRequest httpServletRequest) {
        try {
            byte[] encryptedUsername = Base64.getDecoder().decode(URLDecoder.decode(encryptedData.getEncryptedUsername(), "UTF-8"));
            byte[] encryptedPassword = Base64.getDecoder().decode(URLDecoder.decode(encryptedData.getEncryptedPassword(), "UTF-8"));
            // 使用前端相同的填充方式进行解密
            Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            rsaCipher.init(Cipher.DECRYPT_MODE, RsaUtils.getKeyPair().getPrivate());
            // 对RSA加密解密
            String username = new String(rsaCipher.doFinal(encryptedUsername), StandardCharsets.UTF_8);
            String password = new String(rsaCipher.doFinal(encryptedPassword), StandardCharsets.UTF_8);

            System.out.println("Decrypted username: " + username);
            System.out.println("Decrypted password: " + password);
            if(httpServletRequest.getAttribute("id") == null||!httpServletRequest.getAttribute("id").equals(username)){
                return authService.staffLogin(username, password);
            }
            return ResultMsg.successOp("已登录");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.failOp("出现异常");
    }
    @GetMapping("/public-key")
    public String getPublicKey() {
        if (RsaUtils.getKeyPair() != null) {
            RSAPublicKey publicKey = (RSAPublicKey) RsaUtils.getKeyPair().getPublic();
            byte[] publicKeyBytes = publicKey.getEncoded();
            System.out.println(Base64.getEncoder().encodeToString(publicKeyBytes));
            return Base64.getEncoder().encodeToString(publicKeyBytes);
        } else {
            return "RSA key pair not initialized";
        }
    }
}
